/*
 *   Copyright (c) 2010-2022, SIUFUNG
 */
package net.siufung.market.base.dict.controller.assembler;

import com.google.common.collect.Lists;
import net.siufung.core.utils.CopyUtil;
import net.siufung.market.base.dict.controller.model.dict.DictVo;
import net.siufung.market.base.dict.controller.model.dicttype.DictTypeVo;
import net.siufung.market.base.dict.mapper.entity.Dict;
import net.siufung.market.base.dict.mapper.entity.DictType;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Objects;

/**
 *
 * 字典类型 持久层展示层组装器
 *
 * @author 陈建峰
 * @since 2022-07-08 15:30:16
 */
public class DictTypeAssembler {

    /**
     * DictType 转换成 DictTypeVo
     * @param dictType 数据库实体
     * @return DictTypeVo 视图
     */
    public static DictTypeVo from(DictType dictType){
        if(!Objects.isNull(dictType)){
            DictTypeVo dictTypeVo = new DictTypeVo();
            CopyUtil.copyProperties(dictType, dictTypeVo);
            return dictTypeVo;
        }
        return null;
    }

    /**
     * DictTypeList 转换成 DictTypeVoList
     * @param dictTypeList 数据库实体集合
     * @return List<DictTypeVo> 视图集合
     */
    public static List<DictTypeVo> fromList(List<DictType> dictTypeList){
        List<DictTypeVo> dictTypeVoList = Lists.newArrayList();
        if(!CollectionUtils.isEmpty(dictTypeList)){
            DictTypeVo dictTypeVo;
            for (DictType dictType: dictTypeList) {
                dictTypeVo = new DictTypeVo();
                CopyUtil.copyProperties(dictType, dictTypeVo);
                dictTypeVoList.add(dictTypeVo);
            }
        }
        return dictTypeVoList;
    }

    /**
     * DictTypeList 转换成 DictTypeVoList
     * @param dictTypeList 数据库实体集合
     * @return List<DictTypeVo> 视图集合
     */
    public static List<DictTypeVo> fromList(List<DictType> dictTypeList, List<Dict> dictList){
        List<DictTypeVo> dictTypeVoList = Lists.newArrayList();
        if(!CollectionUtils.isEmpty(dictTypeList)){
            DictTypeVo dictTypeVo;
            List<DictVo> dictVoList;
            for (DictType dictType: dictTypeList) {
                dictTypeVo = new DictTypeVo();
                CopyUtil.copyProperties(dictType, dictTypeVo);
                dictVoList = Lists.newArrayList();
                for (Dict dict: dictList) {
                    if(dictType.getId().equals(dict.getDictTypeId())){
                        dictVoList.add(DictAssembler.from(dict));
                    }
                }
                dictTypeVo.setDictVoList(dictVoList);
                dictTypeVoList.add(dictTypeVo);
            }
        }
        return dictTypeVoList;
    }
}

